<?php $this->JQuery()->addJavascriptFile('http://bp.yahooapis.com/2.4.21/browserplus-min.v-'.AC_VERSION.'.js')?>

    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.v-'.AC_VERSION.'.js'))?>
    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html5.v-'.AC_VERSION.'.js'))?>
    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html4.v-'.AC_VERSION.'.js'))?>

    <?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/jquery.plupload.queue.min.v-'.AC_VERSION.'.js'))?>
<?php if (IS_LIVE):?>
    <?php $this->headLink()->appendStylesheet(Ac_Util::getImageBaseUrl('/css/plupload/plupload.queue.v-'.AC_VERSION.'.css'))?>
<?php endif;?>

<?php $this->JQuery()->javascriptCaptureStart()?>
var uploader;
var dialog;
$j(function() {

    $j('#change_uploader').click(function() {
        if ($j('.form_uploader_lower').css('display') == 'none') {
            $j('#change_uploader').html('<?php echo $this->t('Use the simple uploader')?>');
            $j('#form_uploader_default').hide();
            $j('.form_uploader_lower').removeClass('hide');
            $j('.form_uploader_lower').show();
            $j('.photo_preview').show(); 
        } else {
            
            $j('#change_uploader').html('<?php echo $this->t('Use the fast uploader')?>');
            $j('#form_uploader_default').show();
            $j('.form_uploader_lower').hide();   
			$j('.photo_preview').hide();             
            $j('#form_uploader_default').removeClass('hide');
        }
        return false;
    });
    
    if ($j.browser.msie) {
        $j('#form_uploader_default').show();
        $j('.form_uploader_lower').hide();
        $j('#change_uploader').html('<?php echo $this->t('Use the fast uploader')?>');
    } else {
        $j('#form_uploader_default').hide();
        $j('#form_uploader_default').removeClass('hide');
        $j('.form_uploader_lower').show();
        $j('#change_uploader').html('<?php echo $this->t('Use the simple uploader')?>');
    }

	uploader = new plupload.Uploader({
		runtimes : 'html5,flash,html4',
		browse_button : 'pickfiles',
		container : 'container_uploader',
		max_file_size : '10mb',
		url : '/default/upload/company-photo',
		flash_swf_url : '/plupload/js/plupload.flash.swf',
		silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',
        headers: {},
		filters : [
			{title : "Image files", extensions : "jpeg,jpg,gif,png"}
		]
		
	});

	uploader.bind('Init', function(up, params) {
		//$('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
	});

	$j('#uploadfiles').click(function(e) {
		uploader.start();
		e.preventDefault();
	});

	//uploader.init();
        
        uploader.bind('FilesAdded', function(up, files) {
            $j.each(files, function(i, file) {
                $j('#container_uploader').before(
                        '<div id="' + file.id + '">' +
                        file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');
            });
            up.refresh(); // Reposition Flash/Silverlight        
        });
        
    /*
	uploader.bind('FilesAdded', function(up, files) {
		$j.each(files, function(i, file) {
			$j('#container_uploader').before(
				'<div id="' + file.id + '">' +
				file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
			'</div>');
		});
		up.refresh(); // Reposition Flash/Silverlight
	});
    */

	uploader.bind('UploadProgress', function(up, file) {
            $j('#' + file.id + " b").html(file.percent + "%");
            if (file.percent == 100) {
                $j('#' + file.id).remove();
            }
	});

	uploader.bind('Error', function(up, err) {
                if (err.code == -600) {
                    $j('#container_uploader').before('<ul id="plupload_error" class="errors"><li><?php echo $this->t('The file you are trying to upload is too large')?></li></ul>');
                    $j('#plupload_error').delay(2000).fadeOut();
                    
                } else {
                    $j('#container_uploader').before("<div>Error: " + err.code +
			", Message: " + err.message +
			(err.file ? ", File: " + err.file.name : "") +
			"</div>"
                    );
                }

		up.refresh(); // Reposition Flash/Silverlight
	});

	uploader.bind('FileUploaded', function(up, file, res) {
        var response = $j.parseJSON(res.response);
		$j('#' + file.id + " b").html("100%");
        var nb_photo = $j('.photo_preview').attr("rel");

        if (nb_photo == 0) {
            var div = $j(document.createElement("div")).attr("id", "photo"+nb_photo).attr("class", "selected");
            $j('#no_photo_selected').hide();
        } else {
            var div = $j(document.createElement("div")).attr("id", "photo"+nb_photo);
        }

        $j(document.createElement("img"))
            .attr({ src: response.url})
            .appendTo($j(document.createElement("figure")).appendTo(div));
        $j('.plupload').css('top', '10px');
	    $j('.plupload').css('left', '440px');
        $j(document.createElement("a"))
            .attr({ "class": "remove_photo", rel:nb_photo})
            .html("<?php echo $this->t('Remove')?>")
            .click(function() {
                    var nb = $j(this).attr('rel');
                    $j.post(
                        '<?php echo $this->geturl('delete-photo','user')?>',
                        {num:nb},
                        function (data) {
                            $j('#photo'+nb).remove();
                            $j('.photo_preview').attr("rel", parseInt($j('.photo_preview').attr("rel")) - 1);
                            var i = 0;
                            $j('.remove_photo').each(function() {
                                $j(this).attr('rel', i++);
                            });                        
                            var i = 0;
                            $j('.feature_photo').each(function() {
                                $j(this).attr('rel', i++);
                            });  
                        }
                    );
                    return false;
                })
            .appendTo(div);

        var txt = '';
        if (nb_photo == 0) {
            txt = "<?php echo $this->t('Featured Photo')?>";
        } else {
            txt = "<?php echo $this->t('Feature')?>";
        }

        $j(document.createElement("a"))
        .attr({ "class": "feature_photo", rel:nb_photo})
        .html(txt)
        .click(function() {featurePhoto($j(this), nb_photo);return false;})
        .appendTo(div);

        div.appendTo($j('.photo_preview'));
        $j('.photo_preview').attr("rel", parseInt($j('.photo_preview').attr("rel")) + 1);
	});
    
    uploader.init();

    uploader.bind('FilesAdded', function(up, files) {
        uploader.start();
    });

    $j('.remove_photo').click(function() {
        var nb = $j(this).attr('rel');
        $message = '<?php echo $this->t('Do you want to remove this photo ?')?>';
        if (confirm($message)) {
            $j.post(
                '<?php echo $this->geturl('delete-photo','user')?>',
                {num:nb},
                function (data) {
                        $j('#photo'+nb).remove();
                        $j('.photo_preview').attr("rel", parseInt($j('.photo_preview').attr("rel")) - 1);
                        var i = 0;
                        $j('.remove_photo').each(function() {
                            $j(this).attr('rel', i++);
                        });                        
                        var i = 0;
                        $j('.feature_photo').each(function() {
                            $j(this).attr('rel', i++);
                        });                                                
                }
            );
        }
        return false;
    });

    function featurePhoto(ele, nb) {
        //var nb = ele.attr('rel');
        var obj = ele;

        $j.post(
            '<?php echo $this->geturl('feature-photo','user')?>',
            {id:nb},
            function (data) {
                var i=0;
                $j('.feature_photo').html("<?php echo $this->t('Feature')?>");
                obj.html("<?php echo $this->t('Featured Photo')?>");
            }
        );

        $j('.photo_preview>div').removeClass('selected');
        $j('#photo' + nb).addClass('selected');

        return false;
    }

    $j('.feature_photo').click(function() {featurePhoto($j(this), $j(this).attr('rel')); return false});
    $j('#container_uploader').css('position', 'static');
    //$j('#container_uploader1').removeAttr('position');
    $j('.plupload').css('left', '440px');
    //$j('.plupload').click(function() {
	    
    //});
});
<?php $this->JQuery()->javascriptCaptureEnd()?>
<div id="form_uploader_cnt">
    <div id="form_uploader" class="button form_uploader_lower">
        <label><?php echo $this->t('Upload Photos of Your Business')?></label>
        <span><?php echo $this->t('4MB max each. Format: JPEG, JPG, GIF, PNG')?></span>
        <div id="container_uploader" class="button">
            <button id="pickfiles" type="button"><?php echo $this->t('Browse ...')?></button>
        </div>
    </div>

    <div id="form_uploader_default" class="hide">
        <label><?php echo $this->t('Upload Photos (Simple Mode)')?></label>
        <span><?php echo $this->t('4MB max each. Format: JPEG, JPG, GIF, PNG')?></span>
        <div class="file">
            <label><?php echo $this->t('Featured Photo')?></label>
            <input type="file" id="file_upload_1" name="upload1">
        </div>
        <div class="file">
            <label><?php echo $this->t('Photo 2')?></label>
            <input type="file" id="file_upload_2" name="upload2">    
        </div>
        <div class="file">
            <label><?php echo $this->t('Photo 3')?></label>
            <input type="file" id="file_upload_3" name="upload3">    
        </div>    
        <div class="file">
            <label><?php echo $this->t('Photo 4')?></label>
            <input type="file" id="file_upload_4" name="upload4">    
        </div>    
    </div>
    
    <div rel="<?php echo count($this->company->photos)?>" id="profile_pic_cnt" class="photo_preview">
    <?php if(empty($this->company->photos)):?>
    	<p id="no_photo_selected"><?php echo $this->t('No Photo Selected')?></p>
    <?php endif;?>
    <?php if(!empty($this->company->photos)):?>
        <?php foreach($this->company->photos as $k => $photo):?>
            <div id="photo<?php echo $k?>" <?php if ((int)$this->company->featured_photo_id == $k) { echo "class='selected'"; } ?>>
                <figure>
                    <img src="<?php echo $this->thumbnail()->scale($photo, 150, 150)?>">
                </figure>
                <a href="#" class="remove_photo" rel="<?php echo $k?>"><?php echo $this->t('remove')?></a>
                <?php if ((int)$this->company->featured_photo_id != $k):?>
                    <a href="#" class="feature_photo" rel="<?php echo $k?>">
                         <?php echo $this->t('Feature');?>
                    </a>
                <?php else:?>
                    <a href="#" class="feature_photo" rel="<?php echo $k?>">
                         <?php echo $this->t('Featured photo');?>
                    </a>
                <?php endif;?>
            </div>
        <?php endforeach;?>
    <?php endif;?>  
    </div>
	<p>Unable to upload photos? <a id="change_uploader" href="#">Use the simple uploader</a>.</p>
</div>
